package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkClientGoRankVo;
import com.ruoyi.logistic.domain.vo.NetworkClientGoVo;
import com.ruoyi.logistic.mapper.*;
import com.ruoyi.logistic.service.NetworkClientGoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class NetworkClientGoServiceImpl implements NetworkClientGoService {
    @Autowired
    private NetworkClientGoDetailMapper networkClientGoDetailMapper;
    @Autowired
    private NetworkClientGoCollectMapper networkClientGoCollectMapper;
    @Autowired
    private NetworkClientGoProvinceIncomeRankMapper networkClientGoProvinceIncomeRankMapper;
    @Autowired
    private NetworkClientGoClientIncomeRankMapper networkClientGoClientIncomeRankMapper;
    @Autowired
    private NetworkNameMapper networkNameMapper;
    @Autowired
    private NetworkClientMapper networkClientMapper;
    @Override
    public List<NetworkClientGoDetail> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        List<NetworkClientGoDetail> details = networkClientGoDetailMapper.selectAll();
        for (NetworkClientGoDetail detail : details){
            detail.setNetworkName(networkNameMapper.selectNameById(detail.getNetworkNameId()));
            detail.setClientName(networkClientMapper.selectCodeByName(detail.getClientId()));
            detail.setClientCode(networkClientMapper.selectCodeByName(detail.getClientId()));
            detail.setReceiveSiteName(networkNameMapper.selectNameById(detail.getReceiveSiteNameId()));
        }
        return details;
    }

    @Override
    public Boolean insert(NetworkClientGoDetail networkClientGoDetail) {
        return networkClientGoDetailMapper.insert(networkClientGoDetail);
    }

    @Override
    public Boolean update(NetworkClientGoDetail networkClientGoDetail) {
        return networkClientGoDetailMapper.update(networkClientGoDetail);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return networkClientGoDetailMapper.deleteById(id);
    }

    @Override
    public List<NetworkClientGoDetail> search(NetworkClientGoVo networkClientGoVo) {
        PageHelper.startPage(networkClientGoVo.getPageDate());
        List<NetworkClientGoDetail> details = networkClientGoDetailMapper.search(networkClientGoVo);
        for (NetworkClientGoDetail detail : details){
            detail.setNetworkName(networkNameMapper.selectNameById(detail.getNetworkNameId()));
            detail.setClientName(networkClientMapper.selectCodeByName(detail.getClientId()));
            detail.setClientCode(networkClientMapper.selectCodeByName(detail.getClientId()));
            detail.setReceiveSiteName(networkNameMapper.selectNameById(detail.getReceiveSiteNameId()));
        }
        return details;
    }

    @Override
    public List<NetworkClientGoCollect> clientGoCollect(NetworkClientGoVo networkClientGoVo) {
        PageHelper.startPage(networkClientGoVo.getPageDate());
        List<NetworkClientGoCollect> collects = networkClientGoCollectMapper.clientGoCollect(networkClientGoVo);
        for (NetworkClientGoCollect collect : collects){
            collect.setNetworkName(networkNameMapper.selectNameById(collect.getNetworkNameId()));
            collect.setClientName(networkClientMapper.selectNameById(collect.getClientId()));
        }
        return collects;
    }

    @Override
    public List<NetworkClientGoProvinceIncomeRank> provinceIncomeRank(NetworkClientGoRankVo networkClientGoRankVo) {
        return networkClientGoProvinceIncomeRankMapper.provinceIncomeRank(networkClientGoRankVo);
    }

    @Override
    public NetworkClientGoClientIncomeRankAll clientIncomeRank(NetworkClientGoRankVo networkClientGoRankVo) {
       List<NetworkClientGoClientIncomeRank> list = networkClientGoClientIncomeRankMapper.clientIncomeRank(networkClientGoRankVo);
        List<String> clientNameList = new ArrayList<>();
        List<BigDecimal> incomeList = new ArrayList<>();

        for (NetworkClientGoClientIncomeRank obj : list) {
            clientNameList.add(obj.getNetworkClient());
            incomeList.add(obj.getIncome());
        }

// 然后将集合转换为数组
        String[] clientNames = clientNameList.toArray(new String[0]);
        BigDecimal[] incomes = incomeList.toArray(new BigDecimal[0]);

// 创建并初始化新对象
        NetworkClientGoClientIncomeRankAll all = new NetworkClientGoClientIncomeRankAll();
        all.setClientName(clientNames);
        all.setIncome(incomes);
        return all;
    }
}
